package com.bitejiuyeke.forum.controller;

import com.bitejiuyeke.forum.common.AppResult;
import com.bitejiuyeke.forum.common.ResultCode;
import com.bitejiuyeke.forum.config.AppConfig;
import com.bitejiuyeke.forum.model.Article;
import com.bitejiuyeke.forum.model.ArticleReply;
import com.bitejiuyeke.forum.model.User;
import com.bitejiuyeke.forum.services.IArticleReplyService;
import com.bitejiuyeke.forum.services.IArticleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author 比特就业课
 */
@Api(tags = "回复接口")
@Slf4j
@RestController
@RequestMapping("/reply")
public class ArticleReplyController {

    @Resource
    private IArticleReplyService replyService;
    @Resource
    private IArticleService articleService;

    @ApiOperation("获取回复列表")
    @GetMapping("/getReplies")
    public AppResult<List<ArticleReply>> getRepliesByArticleId (@ApiParam("帖子Id") @RequestParam("articleId") @NonNull Long articleId) {
        // 根据帖子Id查询出帖子详情，校验帖子是否有效
        Article article = articleService.selectById(articleId);
        // 帖子是否存在
        if (article == null || article.getDeleteState() == 1) {
            // 返回错误提示
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        // 帖子是否有效(封帖)
        if (article.getState() == 1) {
            // 返回错误提示
            return AppResult.failed("帖子已封帖");
        }

        // 调用Service，查询回复列表
        List<ArticleReply> articleReplies = replyService.selectByArticleId(articleId);
        if (articleReplies == null) {
            articleReplies = new ArrayList<>();
        }
        // 返回结果
        return AppResult.success(articleReplies);
    }

    @ApiOperation("发布回复")
    @PostMapping("/create")
    public AppResult create (HttpServletRequest request,
                             @ApiParam("帖子Id") @RequestParam("articleId") @NonNull Long articleId,
                             @ApiParam("回复内容") @RequestParam("content") @NonNull String content) {
        // 获取Session对象
        HttpSession session = request.getSession(false);
        // 获取用户对象
        User user = (User) session.getAttribute(AppConfig.SESSION_USER_KEY);
        // 校验用户状态
        if (user.getState() == 1) {
            // 返回提示
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        // 查询帖子信息
        Article article = articleService.selectById(articleId);
        // 校验帖子有效性
        if (article == null || article.getDeleteState() == 1) {
            // 返回提示
            return AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        // 校验帖子状态
        if (article.getState() == 1) {
            // 返回提示
            return AppResult.failed(ResultCode.FAILED_ARTICLE_STATE);
        }
        // 构造Service需要的参数
        ArticleReply articleReply = new ArticleReply();
        articleReply.setPostUserId(user.getId()); // 发表回复的作者Id
        articleReply.setArticleId(articleId); // 帖子Id
        articleReply.setContent(content); // 回复内容
        // 调用Service方法
        replyService.create(articleReply);
        // 返回结果
        return AppResult.success("回复成功");
    }
}
